package cn.dogger.easyboot.redis.redistemplate;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public record RedisUtils<k, v>(RedisTemplate<k, v> redisTemplate) {

    /**
     * 是否key值
     *
     * @param key key值
     * @return ture|false
     */
    public Boolean hasKey(k key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 获取Map值
     *
     * @param key key值
     * @return value
     */
    public Map<Object, Object> getMap(k key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 获取String值
     *
     * @param key key值
     * @return value
     */
    public String getString(k key) {
        return String.valueOf(redisTemplate.opsForValue().get(key));
    }

    /**
     * 添加String
     *
     * @param key key值
     * @return value
     */
    public void setString(k key, v str, long expireTime) {
        if (expireTime == 0) {
            redisTemplate.opsForValue().set(key, str);
            persistKey(key);
        } else {
            redisTemplate.opsForValue().set(key, str);
            setKeyExpire(key, expireTime);
        }
    }

    /**
     * 根据key删除数据
     *
     * @param keys key数组
     */
    @SafeVarargs
    public final void delete(k... keys) {
        redisTemplate.delete(Arrays.asList(keys));
    }

    /**
     * 指定key失效时间
     *
     * @param key  key
     * @param time 时间,分钟
     */
    public void setKeyExpire(k key, long time) {
        redisTemplate.expire(key, time, TimeUnit.MINUTES);
    }

    /**
     * 获取失效时间
     *
     * @param key key
     * @return 剩余时间, 秒
     */
    public Long getKeyExpire(k key) {
        return redisTemplate.getExpire(key);
    }


    /**
     * 将key持久化保存
     *
     * @param key key
     */
    public void persistKey(k key) {
        redisTemplate.persist(key);
    }

    /**
     * 添加map
     *
     * @param key        key
     * @param map        map
     * @param expireTime 超时时间,0为永不超时
     */
    public void setMap(k key, Map<String, Object> map, long expireTime) {
        redisTemplate.opsForHash().putAll(key, map);
        if (expireTime == 0) {
            redisTemplate.persist(key);
        } else {
            setKeyExpire(key, expireTime);
        }
    }

    /**
     * 添加集合
     *
     * @param key        key
     * @param list       集合
     * @param expireTime 超时时间,0为永不超时
     */
    public void setList(k key, long expireTime, List<v> list) {
        redisTemplate.opsForList().leftPushAll(key, list);
        if (expireTime == 0) {
            redisTemplate.persist(key);
        } else {
            setKeyExpire(key, expireTime);
        }

    }

    /**
     * 取集合
     *
     * @param key        key
     * @param startIndex 开始的index,从0开始
     * @param endIndex   结束的index,-1为取到最后
     */
    public List<v> getList(k key, long startIndex, long endIndex) {
        return redisTemplate.opsForList().range(key, startIndex, endIndex);
    }
}
